﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 扫雷核心逻辑
/// 负责场景和地雷的生成、计算
/// </summary>
public class M_GridManager : MonoBehaviour {

    

    /// <summary>
    /// 管理器初始化
    /// </summary>
    public M_GridManager InitGridManager() {
        return this;
    }

    /// <summary>
    /// 生成地雷坐标
    /// </summary>
    /// <param name="count">生成数量</param>
    public void MineCreator(int count) {
        if (count >= M_GameManager.Instance.mineCommonData.mineEntityList.Count) return;

        int _count = 0;
        while (_count < count) {
            E_mineCube e_Mine = M_GameManager.Instance.mineCommonData.getRandomMine();
            if (e_Mine != null && e_Mine.mineCubeData.mineType != D_mineType.Mine) {
                e_Mine.mineCubeData.mineType = D_mineType.Mine;
                _count++;
            }
        }
    }

    /// <summary>
    /// 网格构建
    /// </summary>
    public void GridCreator(int width , int height) {

        if (width <= 0 || height <= 0) {
            Debug.LogError("构建网格失败");
        }

        var widthStart = 0 - (width / 2.0f) + 0.5f;
        var heightStart = 0 - (height / 2.0f) + 0.5f;

        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                //生成默认数据
                var mineData = new C_mineCubeData(new Vector2Int(j, i));
                //生成方块
                var mineCube = M_GameManager.Instance.poolManager.InstantiatePoolObject<E_mineCube>("MineCube", new Vector2(j + widthStart, i + heightStart)).Init(mineData);
                //添加到列表中
                M_GameManager.Instance.mineCommonData.mineEntityList.Add(mineCube);
            }
        }
    }

    public void GridClear() {
        M_GameManager.Instance.poolManager.DisableObjectWitchTable("MineCube");
        M_GameManager.Instance.mineCommonData.mineEntityList.Clear();
    }

    /// <summary>
    /// 计算指定位置附近地雷数量
    /// </summary>
    public void CountMine(Vector2Int point) {
        if (M_GameManager.Instance.mineCommonData.getMineCubeWithPoint(point) == null) return;
        int mineCount = 0;
        //-----------------周围地雷检测
        //正左
        mineCount += (M_GameManager.Instance.mineCommonData.getMineCubeWithPoint(point + Vector2Int.left)?.mineCubeData.mineType == D_mineType.Mine) ? 1 : 0;
        //左上
        mineCount += (M_GameManager.Instance.mineCommonData.getMineCubeWithPoint(point + Vector2Int.left + Vector2Int.up)?.mineCubeData.mineType == D_mineType.Mine) ? 1 : 0;
        //左下
        mineCount += (M_GameManager.Instance.mineCommonData.getMineCubeWithPoint(point + Vector2Int.left + Vector2Int.down)?.mineCubeData.mineType == D_mineType.Mine) ? 1 : 0;
        //正上
        mineCount += (M_GameManager.Instance.mineCommonData.getMineCubeWithPoint(point + Vector2Int.up)?.mineCubeData.mineType == D_mineType.Mine) ? 1 : 0;
        //正下
        mineCount += (M_GameManager.Instance.mineCommonData.getMineCubeWithPoint(point + Vector2Int.down)?.mineCubeData.mineType == D_mineType.Mine) ? 1 : 0;
        //右上
        mineCount += (M_GameManager.Instance.mineCommonData.getMineCubeWithPoint(point + Vector2Int.right + Vector2Int.up)?.mineCubeData.mineType == D_mineType.Mine) ? 1 : 0;
        //右下
        mineCount += (M_GameManager.Instance.mineCommonData.getMineCubeWithPoint(point + Vector2Int.right + Vector2Int.down)?.mineCubeData.mineType == D_mineType.Mine) ? 1 : 0;
        //正右
        mineCount += (M_GameManager.Instance.mineCommonData.getMineCubeWithPoint(point + Vector2Int.right)?.mineCubeData.mineType == D_mineType.Mine) ? 1 : 0;
        //------------------------------

        //赋值
        M_GameManager.Instance.mineCommonData.getMineCubeWithPoint(point).mineCubeData.mineCount = mineCount;
    }

}
